// pages/index/index.js
var app = getApp()
var api = require("../../conf/api")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    news_list:[],
    evennumber:[],
    oddnumber :[],
    min_id:0,
    max_id:0,
    menuList:[{
      "text":"精品文章",
      "img":"/static/images/icon/home_essays_icon_show.png"
    },
    {
      "text":"小视频",
      "img":"/static/images/icon/home_superstar_icon_show.png"
    },
    {
      "text":"名人堂",
      "img":"/static/images/icon/home_video_icon_show.png"
    },
    {
      "text":"观点说",
      "img":"/static/images/icon/home_viewpoint_icon_show.png"
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    wx.request({
      url:api.newsURL ,
      header:{
        Authorization:app.globalData.userInfo ? app.globalData.userInfo.token : null
    },
      method:"GET",
      dataType:"json",
      responseType:"text",
      success(res){
        // 获取排序好的数据进行瀑布流展示
        that.Judge_parity(res.data)
        that.data.max_id=res.data[0].id
        that.data.news_list=res.data
      },
      fail(err){
        console.log(err)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function (options) {
      
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
    let that = this
    wx.request({
      url: api.newsURL,
      data:{
        max_id:this.data.max_id
      },
      header:{
        Authorization:app.globalData.userInfo ? app.globalData.userInfo.token : null
      },
      method:"GET",
      dataType:"json",
      responseType:'text',
      success(res){
       
        let data_list = res.data.reverse()
        if(!res.data.length){
          wx.showToast({
            title: '已是最新数据',
            icon:"none"
          })
          wx.stopPullDownRefresh()
          return
        }
        else{
          wx.showLoading({
            title: '加载中',
          })
        }
        setTimeout(function () {
          wx.hideLoading()
          that.Latest_data(data_list)
          // 关闭上拉
          wx.stopPullDownRefresh()
        }, 600)
     
      }
    })
   
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {  
    let that = this
    wx.request({
      url: api.newsURL,
      data:{
        min_id:this.data.min_id
      },
      header:{
        Authorization:app.globalData.userInfo ? app.globalData.userInfo.token : null
      }, 
      method:"GET",
      dataType:"json",
      responseType:'text',
      success(res){
        if(!res.data.length){
          wx.showToast({
            title: '已到底部',
            icon:"none"
          })
          return
        }
        else{
          wx.showLoading({
            title: '加载中',
          })
        }
        setTimeout(function () {   
          wx.hideLoading()
          that.data.news_list=that.data.news_list.concat(res.data)
          that.Judge_parity(res.data)
         
        }, 500)
        
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 判断奇偶展示瀑布流(需要一个数据列表)
  Judge_parity(args){
    for(let i=0;i<args.length;i++){
      if(i%2==0){
        this.data.evennumber.push(args[i])
      }else{
        this.data.oddnumber.push(args[i])
      }
    }
    this.setData({
      oddnumber:this.data.oddnumber,
      evennumber:this.data.evennumber,
      min_id:args[args.length-1].id,

    })
  },

  Latest_data(args){
    this.data.oddnumber=[]
    this.data.evennumber=[]
    let data_list = args.concat(this.data.news_list)
    for(let i=0;i<data_list.length;i++){
      if(i%2==0){
        this.data.evennumber.push(data_list[i])
      }else{
        this.data.oddnumber.push(data_list[i])
      }
    }
    this.setData({
      oddnumber:this.data.oddnumber,
      evennumber:this.data.evennumber,
      max_id:args[0].id
    })
  }

})
